﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace UdpReceive
{
    /// <summary>
    /// 数据相关工具类
    /// </summary>
    public static class DataUtility
    {
        /// <summary>
        /// 作用：将字符串内容转化为16进制数据编码，其逆过程是HexDecode
        /// </summary>
        /// <param name="strEncode">需要转化的原始字符串</param>
        /// <returns></returns>
        public static string HexEncode(string strEncode)
        {
            string strReturn = "";//  存储转换后的编码
            foreach (short shortx in strEncode.ToCharArray())
            {
                strReturn += shortx.ToString("X2");
            }
            return strReturn;
        }
        /// <summary>
        /// 字符串转16进制字节数组
        /// </summary>
        /// <param name="hexString"></param>
        /// <returns></returns>
        public static byte[] StrToToHexByte(string hexString)
        {
            hexString = hexString.Replace(" ", "");
            if ((hexString.Length % 2) != 0)
                hexString += " ";
            byte[] returnBytes = new byte[hexString.Length / 2];
            for (int i = 0; i < returnBytes.Length; i++)
                returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
            return returnBytes;
        }
        ///// <summary>
        ///// 字节数组转16进制字符串
        ///// </summary>
        ///// <param name="bytes"></param>
        ///// <returns></returns>
        //public static string ByteToHexStr(byte[] bytes)
        //{
        //    string returnStr = "";
        //    if (bytes != null)
        //    {
        //        for (int i = 0; i < bytes.Length; i++)
        //        {
        //            returnStr += bytes[i].ToString("X2");
        //        }
        //    }
        //    return returnStr;
        //}
        /// <summary>
        ///作用：将16进制数据编码转化为字符串，是HexEncode的逆过程
        /// </summary>
        /// <param name="strDecode"></param>
        /// <returns></returns>
        public static string HexDecode(string strDecode)
        {
            string sResult = "";
            for (int i = 0; i < strDecode.Length / 4; i++)
            {
                sResult += (char)short.Parse(strDecode.Substring(i * 4, 4), global::System.Globalization.NumberStyles.HexNumber);
            }
            return sResult;
        }
        /// <summary>
        /// 十六进制字符串转换成字节数组 
        /// </summary>
        /// <param name="hexstr"></param>
        /// <returns></returns>
        public static byte[] HexString2Bytes(String hexstr)
        {
            byte[] b = new byte[hexstr.Length / 2];
            int j = 0;
            for (int i = 0; i < b.Length; i++)
            {
                char c0 = hexstr[j++];
                char c1 = hexstr[j++];
                b[i] = (byte)((Parse(c0) << 4) | Parse(c1));
            }
            return b;
        }
        /// <summary>
        /// 16进制char转换成整型 
        /// </summary>
        /// <param name="c"></param>
        /// <returns></returns>
        public static int Parse(char c)
        {
            if (c >= 'a')
                return (c - 'a' + 10) & 0x0f;
            if (c >= 'A')
                return (c - 'A' + 10) & 0x0f;
            return (c - '0') & 0x0f;
        }

        /// <summary>
        /// 字节数组转为十六进制字符串 
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string ByteArrayToHexString(byte[] data, int length)
        {
            StringBuilder sb = new StringBuilder(data.Length * 3);
            for (int i = 0; i < length; i++)
            {
                sb.Append(data[i].ToString("X2").PadRight(3, ' '));
                //sb.Append(Convert.ToString(data[i], 16).PadLeft(2, '0'));
                //sb.Append(Convert.ToString(data[i], 16).PadLeft(2, '0').PadRight(3, ' '));
            }
            //foreach (byte b in data)
            //{
            //    sb.Append(Convert.ToString(b, 16).PadLeft(2, '0').PadRight(3, ' '));
            //}
            return sb.ToString().ToUpper();
        }
    }
}
